package service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import data.AppConstants;

public class ConversionService
{

	private static final SimpleDateFormat formatDateTime = new SimpleDateFormat(
			AppConstants.DATE_STORAGE);
	private static final SimpleDateFormat formatDate = new SimpleDateFormat(
			AppConstants.DATE);
	
	// ********************************************************************************************************** start
    // ** public methods ***	
	
	/**
	 * @param strDate
	 * @return calendar
	 * @throws DateFormatException
	 */
	public Calendar stringToDate(final String strDate)
	{

		try 
		{		
			Calendar calendar = Calendar.getInstance();
			formatDateTime.setLenient(false);
			calendar.setTime(formatDateTime.parse(strDate));
			
			return calendar;
			
		} catch (ParseException e) 
		{
			System.out.println(e.getMessage());
			System.out.println("Format day.month.year hour:minute");
		}
		
		return null;
		
	}
	
	/**
	 * @param strDate
	 * @return
	 */
	public Calendar stringToDay(final String strDate)
	{

		try 
		{		
			Calendar calendar = Calendar.getInstance();
			formatDate.setLenient(false);
			calendar.setTime(formatDate.parse(strDate));
			
			return calendar;
			
		} catch (ParseException e) 
		{
			System.out.println(e.getMessage());
		}
		
		return null;
		
	}
	
	/**
	 * @param calendar
	 * @return string
	 */
	public String dateToString(final Calendar calendar)
	{

		return (calendar != null) ? formatDateTime.format(calendar.getTime()) : "";
	}
	
	// ********************************************************************************************************** end

	
}
